package cn.hysh.appium.util;

import org.openqa.selenium.By;

public class GetByLocator {
	
	public static By getLocator(String key)  {
		ProUtil properties=new ProUtil("configs/element.properties");
		//根据变量 ElementNameInpropFile，从属性配置文件中读取对应的配置对象
		//locator=name>登录或注册
        String locator = properties.getPro(key);
        String locatorType = locator.split(">")[0];
        String locatorValue = locator.split(">")[1];
        System.out.println("获取的定位类型：" + locatorType + "\t 获取的定位表达式" + locatorValue );
		if(locatorType.toLowerCase().equals("id"))
			return By.id(locatorValue);
		else if(locatorType.toLowerCase().equals("name"))
			return By.name(locatorValue);//By.name("登录或注册")
		else if((locatorType.toLowerCase().equals("classname"))||(locatorType.toLowerCase().equals("class")))
			return By.className(locatorValue);
		else if((locatorType.toLowerCase().equals("tagname"))||(locatorType.toLowerCase().equals("tag")))
			return By.className(locatorValue);
		else if((locatorType.toLowerCase().equals("linktext"))||(locatorType.toLowerCase().equals("link")))
			return By.linkText(locatorValue);
		else if(locatorType.toLowerCase().equals("partiallinktext"))
			return By.partialLinkText(locatorValue);
		else if((locatorType.toLowerCase().equals("cssselector"))||(locatorType.toLowerCase().equals("css")))
			return By.cssSelector(locatorValue);
		else if(locatorType.toLowerCase().equals("xpath"))
			return By.xpath(locatorValue);
		else
			try {
				throw new Exception("输入的 locator type 未在程序中被定义：" + locatorType );
			} catch (Exception e) {
				e.printStackTrace();
			}
		return null;
    }
}
